<?php session_start();
include('header.php');
require('userManager.php');

$regexp = "/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/";

if(!isset($_POST[firstName]))
    $errorMessage =  "You failed to enter your first name.";
else if(!isset($_POST[username]))
    $errorMessage = "You failed to enter a username.";
else if(!isset($_POST[password]))
    $errorMessage = "You failed to enter a password.";
else if(!isset($_POST[password2]))
    $errorMessage = "You failed to re-enter a password.";
else if($_POST[password] != $_POST[password2])
    $errorMessage = "Your passwords did not match.";
else if(!isset($_POST[email]))
    $errorMessage = "You failed to enter an email.";
else if(!isset($_POST[birthmonth]) || !isset($_POST[birthday]) || !isset($_POST[birthyear]))
    $errorMessage = "You did not enter a proper date.";
else if(isset($_SESSION[userID]))
    $errorMessage = "You are already logged in.";
else if(preg_match($regexp, $_POST[email]))
    $errorMessage = "Email entered in incorrect format.";

if ($errorMessage != "")
    include('register.php');
else
{
    $encrypted = crypt(md5($_POST[password]),md5($_POST[username]));
    $birthdate = "$_POST[birthyear]-$_POST[birthmonth]-$_POST[birthday]";
    $address = $_POST[street] + "\n" + $_POST[city] + ", " + $_POST[state] + " " + $_POST[zip];

    $newUser = new User();
    $newUser->setFName($_POST[firstName]);
    $newUser->setLName($_POST[lastName]);
    $newUser->setUsername($_POST[username]);
    $newUser->setPassword($encrypted);
    $newUser->setEmail($_POST[email]);
    $newUser->setPhone($_POST[phone]);
    $newUser->setAddress($address);
    $newUser->setBirthday($birthdate);

    userManager::registerUser($newUser);


 }


?>
